package com.mune.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.mune.common.exception.BizCodeEnum;
import com.mune.gulimall.member.exception.PhoneExistException;
import com.mune.gulimall.member.exception.UserNameExistException;
import com.mune.gulimall.member.feign.CouponFeignService;
import com.mune.gulimall.member.vo.MemberLoginVo;
import com.mune.gulimall.member.vo.UserRegisterVo;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mune.gulimall.member.entity.MemberEntity;
import com.mune.gulimall.member.service.MemberService;
import com.mune.common.utils.PageUtils;
import com.mune.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * 会员
 *
 * @author mune
 * @email 1356008591@qq.com
 * @date 2020-07-29 10:06:25
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
  @Autowired
  private MemberService memberService;
  @Autowired
  private CouponFeignService couponFeignService;

  @RequestMapping("/coupons")
  public R Test() {
    MemberEntity memberEntity = new MemberEntity();
    memberEntity.setNickname("张三");
    R memberCoupons = couponFeignService.memberCoupons();
    return R.ok().put("member", memberEntity).put("coupons", memberCoupons.get("coupons"));
  }

  /**
   * 列表
   */
  @RequestMapping("/list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = memberService.queryPage(params);

    return R.ok().put("page", page);
  }

  @PostMapping("/register")
  public R register(@RequestBody UserRegisterVo userRegisterVo) {
    try {
      memberService.register(userRegisterVo);
    } catch (PhoneExistException e) {
      e.printStackTrace();
      return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
    } catch (UserNameExistException e) {
      e.printStackTrace();
      return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
    }
    return R.ok();
  }

  @PostMapping("/login")
  public R login(@RequestBody MemberLoginVo vo) {
    MemberEntity entity = memberService.login(vo);
    if (entity == null)
      return R.error(BizCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getCode(), BizCodeEnum.LOGINACCT_PASSWORD_INVALID_EXCEPTION.getMessage());
    return R.ok().setData(entity);
  }

  /**
   * 信息
   */
  @RequestMapping("/info/{id}")
  public R info(@PathVariable("id") Long id) {
    MemberEntity member = memberService.getById(id);

    return R.ok().put("member", member);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  public R save(@RequestBody MemberEntity member) {
    memberService.save(member);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  public R update(@RequestBody MemberEntity member) {
    memberService.updateById(member);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] ids) {
    memberService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

}
